package com.example.fence.entity;

import com.example.fence.enums.AlarmMode;
import com.example.fence.util.Result;
import com.example.fence.vo.AlarmVo;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import static com.example.fence.util.ResultCode.FAIL;

@Data

public class Alarm {
    // 设备imei列表
    private List<String> devicesName;
    // 报警类型
    private Set<Integer> alarmMode;
    // 发送邮箱列表
    private String sendEmail;
    // 接收邮箱列表
    private Set<String> receiveEmail;
    // 邮件传输间隔
    private int sendInterval;

    // AlarmVo转换为Alarm类型
    public Alarm(AlarmVo alarmVo){

        // 设备列表
        this.devicesName=alarmVo.getDevicesName();
        // 报警类型
        this.alarmMode=new HashSet<>();
        for (String mode : alarmVo.getAlarmMode()) {  // 遍历上传的报警类型判断是否都在报警列表中
            for (AlarmMode alarm : AlarmMode.values()) {  // 遍历枚举列表依次判断是否符合
                if (mode.equalsIgnoreCase(alarm.name())) {  // 在枚举列表中
                    alarmMode.add(alarm.code);
                    break;  // 进行下一个传入参数的判断
                }
            }
        }
        // 发送邮箱
        if(alarmVo.getSendEmail()!=null && !alarmVo.getSendEmail().isEmpty()){
            this.sendEmail=alarmVo.getSendEmail().get(0).toLowerCase();
        }

        // 接收邮箱
        if(alarmVo.getReceiveEmail()!=null && !alarmVo.getReceiveEmail().isEmpty()){
            this.receiveEmail=new HashSet<>();
            for(String email:alarmVo.getReceiveEmail()){
                this.receiveEmail.add(email.toLowerCase());
            }
        }
        // 邮件传输间隔
        if(alarmVo.getSendInterval()>=10&&alarmVo.getSendInterval()<=86400){
            this.sendInterval=alarmVo.getSendInterval();
        }

    }
    public static void main(String[] args) {

        List<String> send=new LinkedList<>();
        List<String> re=new LinkedList<>();
        send.add("123");
        send.add("456");
        re.add("Abc");
        re.add("abc");
        re.add("XYZ@124");
        List<String> modeList=new LinkedList<>();
        AlarmVo alarmVo=new AlarmVo(null,modeList,send,re);

        modeList.add("email");
        System.out.println(alarmVo.validateAlarmModes());
        System.out.println(new Alarm(alarmVo).toString());

        modeList.add("lock");
        System.out.println(alarmVo.validateAlarmModes());
        System.out.println(new Alarm(alarmVo).toString());

        modeList.add("LOck");
        System.out.println(alarmVo.validateAlarmModes());
        System.out.println(new Alarm(alarmVo).toString());

        modeList.add("wrong");
        System.out.println(alarmVo.validateAlarmModes());
        System.out.println(new Alarm(alarmVo).toString());


    }

}


